<template>
  <div class="login">
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form demo-ruleForm">
      <h3 class="title">ERP后台管理系统</h3>
      <el-form-item style="margin-bottom: 20px" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入账号"/>
      </el-form-item>
      <el-form-item style="margin-bottom: 20px" prop="password">
        <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin:0px 0px 25px 0px;"
            label="记住密码"
            size="large">
        </el-checkbox>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
            :loading="loading"
            size="large"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
<!--      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>-->
    </div>
  </div>
</template>

<script>
import {ref, reactive, computed,getCurrentInstance} from "vue"
import {useRouter} from "vue-router"
import {useStore} from "vuex"
import {login} from "@/api/login";
import {setToken} from "@/utils/auth";
import Fingerprint2 from 'fingerprintjs2'
import FingerprintJS from '@fingerprintjs/fingerprintjs'

export default {
  name: "login",
  components: {},
  data() {
    return {}
  },
  setup() {
    const router = useRouter();
    const store = useStore();
    const loginFormRef = ref();
    const loading = ref(false);
    const form = reactive({
          loginForm: {
            username: "admin",
            password: "123456789",
            rememberMe: true
          },
          loginRules: {
            username: [{required: true, message: '请输入账号', trigger: 'blur'}],
            password: [{required: true, message: "请输入密码", trigger: 'blur'}],
          }
        }
    );
    const handleLogin = () => {
      //表单验证
      loginFormRef.value.validate((valid) => {
        if (valid) {
          loading.value = true;
          //获取store
          console.log("store",store.state)
          store.dispatch('user/userLogin', form.loginForm).then(res=>{
            loading.value = false;
            router.push({
              path: "/index",
            })
          }).catch(()=>{
            loading.value = false;
          })
        }
      })
    }
    // 获取浏览器的唯一标识符
    const createFingerprint = () => {
      // 浏览器指纹
      const fingerprint = Fingerprint2.get((components) => {
        // 参数只有回调函数时，默认浏览器指纹依据所有配置信息进行生成
        const values = components.map((component) => component.value) // 配置的值的数组
        const murmur = Fingerprint2.x64hash128(values.join(''), 31) // 生成浏览器指纹
        console.log( murmur, '浏览器指纹')
        //murmur就是浏览器指纹啦
        // this.murmur = murmur
      })
      /* FingerprintJS.load().then(fp => {
         fp.get().then(result => {
           const visitorId = result.visitorId;
           console.log('获取设备唯一标识：',visitorId);
         });
       });*/

    }
    const systemId = getCurrentInstance()?.appContext.config.globalProperties.$VERSION
    console.log(systemId, '获取浏览器唯一标识');//控制台可以看到输出了10

    createFingerprint();
    return {
      ...form,
      loginFormRef,
      /*loginForm,
      loginRules,*/
      loading,
      handleLogin,

    }
  },
  created() {
  },
  mounted() {
  },

  methods: {
    /*handleLogin(){
      login(this.loginForm).then(res=>{
        console.log("login",res)
      })
    },*/
  },
  computed: {},
  watch: {},
}
</script>

<style scoped lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("@/assets/images/login-background.jpeg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>